<?php
/**
 *
 * @package    opensocial
 * @subpackage dataresponse
 * @author     Daniel Botelho <botelho.daniel@gmail.com>
 */
class OSDataResponse
{
	private $container_request;
	
	public function getJSFunction()
	{
		return $this->container_request->getDataResponseFunction();
	}
	
	/**
	 * Get's the data request that has generate this response
	 *
	 * @return OSDataRequest The data request that has generate this response
	 */
	public function getDataRequest()
	{
		return $this->container_request;
	}
	
	function __construct(OSDataRequest  $container_request)
	{
		$this->container_request = $container_request;
	}	
	/**
	 * This method returns the data fecthed by the container. By default it initializes the variable.
	 * @param OSDataRequestable $data_request The requested data
	 * @param boolean $initialize By default it initializes the variable
	 */
	public function getData(OSDataRequestable $data_request,$initialize=true,$appendable=false,$check_init=false)
	{
	  if($initialize){
	    if($check_init){
	      return sprintf("var %s = (%s.get(\"%s\"))? %s.get(\"%s\").getData():null;\n",$data_request->getOptionalKey(),$this->getJSFunction()->getArgs(),$data_request->getOptionalKey(),$this->getJSFunction()->getArgs(),$data_request->getOptionalKey());
	    }else{
	      return sprintf("var %s = %s.get(\"%s\").getData();\n",$data_request->getOptionalKey(),$this->getJSFunction()->getArgs(),$data_request->getOptionalKey());
	    }
	    
	  }else{
	    if($check_init){
	      return StringUtil::appendableString(sprintf("(%s.get(\"%s\"))? %s.get(\"%s\").getData():null",$this->getJSFunction()->getArgs(),$data_request->getOptionalKey(),$this->getJSFunction()->getArgs(),$data_request->getOptionalKey()),$appendable);
	    }else{
	      return StringUtil::appendableString(sprintf("%s.get(\"%s\").getData()",$this->getJSFunction()->getArgs(),$data_request->getOptionalKey()),$appendable);
	    }
	  }
	   
	    
	}
	
	public function getErrorCode(OSDataRequestable $data_request,$appendable=false)
	{
		return StringUtil::appendableString(sprintf("%s.get(\"%s\").getErrorCode()",$this->getJSFunction()->getArgs(),$data_request->getOptionalKey()),$appendable);
	}
	
	public function getErrorMessage(OSDataRequestable $data_request,$appendable=false)
	{
		return StringUtil::appendableString(sprintf("%s.get(\"%s\").getErrorMessage()",$this->getJSFunction()->getArgs(),$data_request->getOptionalKey()),$appendable);
	}
	
	public function getOriginalDataRequest(OSDataRequestable $data_request,$appendable=false)
	{
		return StringUtil::appendableString(sprintf("%s.get(\"%s\").getOriginalDataRequest()",$this->getJSFunction()->getArgs(),$data_request->getOptionalKey()),$appendable);
	}
	
    public function hadError(OSDataRequestable $data_request = null,$appendable=false)
	{
		if($data_request)
		  return StringUtil::appendableString(sprintf('%s.get("%s").hadError()',$this->getJSFunction()->getArgs(),$data_request->getOptionalKey()),$appendable);
		else
		  return StringUtil::appendableString($this->getJSFunction()->getArgs().'.hadError()',$appendable);
	}
}